﻿using BaseDll;
using CommonTools;
using Entity;
using HWindowToolcs;
using StationDemo.utils;
using StationDemo.自定义界面;
using Sunny.UI;
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using UPH;
using UserData;

namespace StationDemo
{
    public partial class Form_Auto : UIPage
    {
        private AutoSizeFormClass asc = new AutoSizeFormClass();

        


        #region  参数定义
        public delegate void ShowImage(int falg, Bitmap s);
        public static ShowImage showImage;
        //public delegate void ShowSN(int flag, string SN);
        //public static ShowSN showSN;
        public delegate void FlashEndStatus(bool status);
        public static FlashEndStatus flashEndStatus;





        //public delegate void FlashProductData(ProductData testResult);
        //public static FlashProductData flashProductData;
        public delegate void EventShowSN(string sn);//
        public static EventShowSN ShowSN;
        public delegate void EventGetSN(ref string sn);//
        public static EventGetSN GetSN;
        public delegate void EventFlashResult(int Result);//
        public static EventFlashResult FlashResult;

        #endregion

        public Form_Auto()
        {
            InitializeComponent();
            asc.controllInitializeSize(this);
        }
        private void Function_遍历控件(Control control)
        {

            foreach (var item in control.Controls)
            {
                if (item is Interface_UserControl)
                {
                    ((Interface_UserControl)item).Init();
                }
                else
                {
                    Function_遍历控件((Control)item);
                }
            }


        }

        private void Form_Auto_Load(object sender, EventArgs e)
        {
            #region 初始化参数配置



            Function_遍历控件(this);

            flashEndStatus += Function_刷新退料状态;
            FlashResult += flashResult;
            //专案信息加载
            label_CurrentFile.Text = "当前产品:" + ParamSetMgr.GetInstance().CurrentProductFile;
            txt_机型.Text = Param_System.Instance.机型._strParamVal;
            txt_批次.Text = Param_System.Instance.批次._strParamVal;
            txt_操作者.Text = Param_System.Instance.操作者._strParamVal;
            txt_设备编号.Text = Param_System.Instance.设备编号._strParamVal;
            txt_工站编号.Text = Param_System.Instance.工站编号._strParamVal;
            #endregion

            Station_Table station = (Station_Table)StationMgr.GetInstance().GetStation(Station.转盘站.ToString());

            Station_标定站 station_标定 = (Station_标定站)StationMgr.GetInstance().GetStation(Station.标定站.ToString());
            station_标定.FlashProductData += Function_刷新数据;
            station_标定.FuncSetExternSoft += SetExternSoft;

            Form_NGControl form_NGA = new Form_NGControl(Station.治具A站.ToString());
            form_NGA.Dock = DockStyle.Fill;
            form_NGA.Show();
            tableLayoutPanel1.Controls.Add(form_NGA);

            Form_NGControl form_NGB = new Form_NGControl(Station.治具B站.ToString());
            form_NGB.Dock = DockStyle.Fill;
            form_NGB.Show();
            tableLayoutPanel1.Controls.Add(form_NGB);

            Form_NGControl form_NGC = new Form_NGControl(Station.标定站.ToString());
            form_NGC.Dock = DockStyle.Fill;
            form_NGC.Show();
            tableLayoutPanel1.Controls.Add(form_NGC);

            LoadSeverMgr.Instance.Init();
        }
        
        private void btn_修改机型_Click(object sender, EventArgs e)
        {
            string value = "";
            string desc = "请输入机型名称：".Trim();
            if (UIInputDialog.InputStringDialog(this, ref value, true, desc))
            {
                if (value != "")
                {
                    txt_机型.Text = value;
                    Param_System.Instance.机型._strParamVal = value;
                    ParamSetMgr.GetInstance().SaveParam(Param_System.Instance);
                }
                else
                {
                    UIMessageBox.ShowError("请输入正确的机型");
                    return;
                }
            }
        }
        private void btn_修改批次_Click(object sender, EventArgs e)
        {
            string value = "";
            string desc = "请输入批次名称：".Trim();
            if (UIInputDialog.InputStringDialog(this, ref value, true, desc))
            {
                if (value != "")
                {
                    txt_批次.Text = value;
                    Param_System.Instance.批次._strParamVal = value;
                    ParamSetMgr.GetInstance().SaveParam(Param_System.Instance);
                }
                else
                {
                    UIMessageBox.ShowError("请输入正确的批次");
                    return;
                }
            }

        }
        private void btn_修改操作者_Click(object sender, EventArgs e)
        {
            string value = "";
            string desc = "请输入操作者名称：".Trim();
            if (UIInputDialog.InputStringDialog(this, ref value, true, desc))
            {
                if (value != "")
                {
                    txt_操作者.Text = value;
                    Param_System.Instance.操作者._strParamVal = value;
                    ParamSetMgr.GetInstance().SaveParam(Param_System.Instance);
                }
                else
                {
                    UIMessageBox.ShowError("请输入正确的操作者");
                    return;
                }
            }
        }
        private void btn_修改设备编号_Click(object sender, EventArgs e)
        {
            string value = "";
            string desc = "请输入设备编号名称：".Trim();
            if (UIInputDialog.InputStringDialog(this, ref value, true, desc))
            {
                if (value != "")
                {
                    txt_设备编号.Text = value;
                    Param_System.Instance.设备编号._strParamVal = value;
                    ParamSetMgr.GetInstance().SaveParam(Param_System.Instance);
                }
                else
                {
                    UIMessageBox.ShowError("请输入正确的设备编号");
                    return;
                }
            }
        }
        private void btn_修改工站编号_Click(object sender, EventArgs e)
        {
            string value = "";
            string desc = "请输入工站编号名称：".Trim();
            if (UIInputDialog.InputStringDialog(this, ref value, true, desc))
            {
                if (value != "")
                {
                    txt_工站编号.Text = value;
                    Param_System.Instance.工站编号._strParamVal = value;
                    ParamSetMgr.GetInstance().SaveParam(Param_System.Instance);
                }
                else
                {
                    UIMessageBox.ShowError("请输入正确的工站编号");
                    return;
                }
            }
        }

        private void Function_刷新退料状态(bool status)
        {
            if (InvokeRequired)
            {
                this.BeginInvoke(new Action(() => Function_刷新退料状态(status)));
            }
            else
            {
                btn_退料.Enabled = status;
            }
        }

        private bool Function_刷新数据(ProductData testResult, bool isStart = false)
        {
            if (InvokeRequired)
            {
                IAsyncResult asyncResult = BeginInvoke(new Func<bool>(() => { return Function_刷新数据(testResult, isStart); }));
                while (!asyncResult.IsCompleted)
                {
                    Thread.Sleep(10);
                }
                // 获取异步操作的结果
                return (bool)EndInvoke(asyncResult);
            }
            else
            {
                bool r = false;
                if (isStart)
                {
                    label_Result.Text = "测试中";
                    label_Result.BackColor = Color.Yellow;
                }
                else
                {
                    r = product_数据刷新.Func_数据对比(testResult);
                    if (r)
                    {
                        label_Result.Text = "OK";
                        label_Result.BackColor = Color.Green;
                    }
                    else
                    {
                        label_Result.Text = "NG";
                        label_Result.BackColor = Color.Red;
                    }
                }
                return r;
            }

        }

        private void btn_退料_Click(object sender, EventArgs e)
        {


            if (!this.ShowAskDialog("是否退料！", UIStyle.Blue)) return;

            UserConfig.b_退料 = true;
            //flashEndStatus(false);

        }

        private void flashResult(int Result)
        {
            if (InvokeRequired)
            {
                this.BeginInvoke(new Action(() => flashResult(Result)));
            }
            else
            {
                //if (socketnum == 0)
                //{
                //    label_SocketNum.Text = "A工位治具";
                //}
                //else if (socketnum == 1)
                //{
                //    label_SocketNum.Text = "B工位治具";
                //}
                //else if (socketnum == 2)
                //{
                //    label_SocketNum.Text = "C工位治具";
                //}
                //else if (socketnum == 3)
                //{
                //    label_SocketNum.Text = "D工位治具";
                //}
                //tabControl1.SelectedIndex = socketnum;

                if (Result == 1)
                {
                    label_Result.Text = "OK";
                    label_Result.ForeColor = Color.Green;
                }
                if (Result == 0)
                {
                    label_Result.Text = "NG";
                    label_Result.ForeColor = Color.Red;
                }
                if (Result == 2)
                {
                    label_Result.Text = "NONE";
                    label_Result.ForeColor = Color.Black;
                }
            }

        }

        private void txt_SN_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                UserConfig.str_SN = this.control_SN1.Function_GetSN();
                // this.lab_ProductSN.Text = $"当前扫码SN：{SysParams.station_上下料.strSN}";
                this.control_SN1.Text = string.Empty;
                UserConfig.b_isScan = true;
            }
        }

        private void uiLabel13_DoubleClick(object sender, EventArgs e)
        {
            UserConfig.str_SN = this.control_SN1.Function_GetSN();
            // this.lab_ProductSN.Text = $"当前扫码SN：{SysParams.station_上下料.strSN}";
            this.control_SN1.Text = string.Empty;
            UserConfig.b_isScan = true;
        }

        private void Form_Auto_Resize(object sender, EventArgs e)
        {
            this.asc.controlAutoSize(this);
        }

        /// <summary>
        /// 将外部程序的窗口加载到当前程序中
        /// </summary>
        /// <param name="mfcWindowHandle">外部程序的句柄</param>
        /// <returns></returns>
        private bool SetExternSoft(IntPtr mfcWindowHandle)
        {
            // 重绘窗口
            SystemDLL.MoveWindow(mfcWindowHandle, 0, 0, defineVisionImage1.Width, defineVisionImage1.Height, true);

            // 将MFC程序窗口嵌入到C#程序的Panel控件中
            SystemDLL.SetParent(mfcWindowHandle, defineVisionImage1.Handle);

            // 调整MFC程序窗口的大小和位置
            SystemDLL.MoveWindow(mfcWindowHandle, 0, 0, defineVisionImage1.Width, defineVisionImage1.Height, true);

            return true;
        }
    }
}
